#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define IPC_PATH "./.fifo"

using namespace std;
int main()
{
    umask(0);
    if (mkfifo(IPC_PATH, 0600) != 0)
    {
        cerr << "mkfifo error" << endl;
        return 1;
    }

    int pipefd = open(IPC_PATH, O_RDONLY);
    if (pipefd == -1)
    {
        cerr << "open error" << endl;
        return 2;
    }
    char buff[1024];
    while (1)
    {
        ssize_t s = read(pipefd, buff, sizeof(buff) - 1);
        if (s > 0)
        {
            buff[s] = '\0';
            cout << "client send message:" << buff << endl;
        }
        else if (s == 0)
        {
            cout << "client exit,server exit too" << endl;
            break;
        }
        else
        {
            cout << "read:" << strerror(errno) << endl;
            break;
        }
    }
    close(pipefd);
    unlink(IPC_PATH);
    return 0;
}
